//
//  MDRefreshHeader.m
//  T_TableViewDemo
//
//  Created by CRY on 16/11/30.
//  Copyright © 2016年 CRY. All rights reserved.
//

#import "MDRefreshHeader.h"

#define MDCOLOR [UIColor purpleColor];

@interface MDRefreshHeader()
{
    UIView *_backgroundView;
    UIView *_controlPoint;
    CAShapeLayer *_shapeLayer;
    BOOL _animation;
    CADisplayLink *_displayLink;
    float _lastOffsetY;
}

@end

@implementation MDRefreshHeader

- (void)setBackgroundView{
    
    _shapeLayer = [CAShapeLayer layer];
    UIColor *mColor = MDCOLOR;
    _shapeLayer.fillColor = mColor.CGColor;
    [self.layer addSublayer:_shapeLayer];
}

- (void)placeSubviews{
    [super placeSubviews];
    
    if (_backgroundView == nil) {
        _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkAction:)];
        [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
        
        _backgroundView = [[UIView alloc] init];
        _backgroundView.backgroundColor = MDCOLOR;
        _controlPoint = [[UIView alloc] init];
        _controlPoint.mj_origin = CGPointMake(self.mj_w/2, self.mj_h);
        [self setBackgroundView];
        //    [_scrollView addSubview:_backgroundView];
        [self addSubview:_backgroundView];
    }
}

- (void)setState:(MJRefreshState)state{
    MJRefreshCheckState
    // 闲置状态
    if (state == MJRefreshStateIdle) {
        
        if (oldState == MJRefreshStatePulling) {
            // 正在刷新转为 闲置状态
            _animation = YES;
            [UIView animateWithDuration:0.9 delay:0 usingSpringWithDamping:0.57f initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                
                _controlPoint.center = CGPointMake(self.mj_w/2 , self.mj_h);
                _shapeLayer.path = [self currentPath].CGPath;
            } completion:^(BOOL finished) {
                _animation = NO;
            }];
        } else {
            // 普通闲置状态
            
        }
    } else if (state == MJRefreshStatePulling) {
        // 下拉状态
        NSLog(@"// 下拉状态");
    } else if (state == MJRefreshStateRefreshing) {
        //  正在刷新状态
        NSLog(@"//  正在刷新状态");
        
    }
}

- (void)displayLinkAction:(CADisplayLink *)dis{
    
    CGFloat offsetY = self.scrollView.mj_offsetY;
//    BOOL scrollUp = _lastOffsetY < offsetY;
    _lastOffsetY = offsetY;
    
    if (_animation == YES) {
        return;
    }
    if (-offsetY < self.mj_h) {
       _controlPoint.center = CGPointMake(self.mj_w/2 , self.mj_h);
    }else{
        _controlPoint.center = CGPointMake(self.mj_w/2 , -offsetY * (offsetY == -self.mj_h? 1: 1.2));
    }
    _shapeLayer.path = [self currentPath].CGPath;
}

- (UIBezierPath *)currentPath{
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0,self.mj_h)];
    [path addQuadCurveToPoint:CGPointMake(self.mj_w,self.mj_h) controlPoint:_controlPoint.center];
    [path addLineToPoint:CGPointMake(self.mj_w, 0)];
    [path addLineToPoint:CGPointMake(0, 0)];
    [path closePath];
    return  path;
}

@end
